package com.xhwl.centiotdata.service.integrateconfig;

import com.xhwl.centiotdata.pushtrigger.alarm.subscribe.AlarmIntegrationConfigEvent;
import com.xhwl.common.dto.cent.alarm.AlarmIntegrationConfigDTO;
import com.xhwl.common.enums.CategoryEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/12/17/ 11:45
 */
@Component
@Slf4j
public class AlarmIntegrationConfig extends IntegrationConfig<Integer, List<AlarmIntegrationConfigDTO>> {

    private Map<Integer, List<AlarmIntegrationConfigDTO>> alarmIntegrationConfigMap = new HashMap<>();

    private List<AlarmIntegrationConfigDTO> alarmIntegrationConfigList = new ArrayList<>();

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public List<AlarmIntegrationConfigDTO> getIntegrationConfig(Integer projectId) {
        return alarmIntegrationConfigMap.get(projectId);
    }

    public List<AlarmIntegrationConfigDTO> getIntegrationProductConfig(Integer projectId, Integer productId) {
        List<AlarmIntegrationConfigDTO> list = Objects.nonNull(projectId)
                ? alarmIntegrationConfigMap.get(projectId)
                : alarmIntegrationConfigList;
        List<AlarmIntegrationConfigDTO> res = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (AlarmIntegrationConfigDTO config : list) {
                if (Objects.equals(productId, config.getProductId())) {
                    res.add(config);
                }
            }
        }
        return res;
    }

    public AlarmIntegrationConfigDTO getIntegrationConfigById(Integer rowId) {
        AlarmIntegrationConfigDTO configDTO = null;
        if (!CollectionUtils.isEmpty(alarmIntegrationConfigList)) {
            for (AlarmIntegrationConfigDTO config : alarmIntegrationConfigList) {
                if (Objects.equals(rowId, config.getRowId())) {
                    configDTO = config;
                    break;
                }
            }
        }
        return configDTO;
    }

    public List<AlarmIntegrationConfigDTO> getAlarmIntegrationConfigList() {
        return alarmIntegrationConfigList;
    }

    @EventListener
    public void listenIntegrationConfig(IntegrationConfigEvent event) {
        this.handleAlarmIntegrationConfig();
    }

    private void handleAlarmIntegrationConfig() {
        alarmIntegrationConfigList = integrationConfigList.stream()
                .filter(deviceProjSupplierCategory -> CategoryEnum.ALARM_SYSTEM.getId().equals(deviceProjSupplierCategory.getProdCategoryId()))
                .map(AlarmIntegrationConfigDTO::toAlarmConfig)
                .collect(Collectors.toList());
        alarmIntegrationConfigMap = alarmIntegrationConfigList.stream()
                .collect(Collectors.groupingByConcurrent(integrationConfig -> integrationConfig.getProjectId()));
        log.info("===== 刷新告警集成配置 alarmIntegrationConfigList：{}", alarmIntegrationConfigList.size());

        AlarmIntegrationConfigEvent event = new AlarmIntegrationConfigEvent();
        applicationEventPublisher.publishEvent(event);
    }
}
